package com.quectel.core.module.ai.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.ai.dao.AiControlLibDao;
import com.quectel.core.module.ai.dao.AiLibApplicationDao;
import com.quectel.core.module.ai.dto.AiControlLibDto;
import com.quectel.core.module.ai.dto.AiLibApplicationDto;
import com.quectel.core.module.ai.entity.AiControlLibEntity;
import com.quectel.core.module.ai.entity.AiLibApplicationEntity;
import com.quectel.core.module.ai.service.AiControlLibService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author rananxin
 * @date 2021-11-24 19:13:28
 */
@DubboService
public class AiControlLibServiceImpl implements AiControlLibService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AiControlLibServiceImpl.class);

    private static final String AI_CONTROL_LIB_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "AI_CONTROL_LIB_CACHE_BY_ID:";

    @Autowired
    private AiControlLibDao aiControlLibDao;
    @Autowired
    private AiLibApplicationDao aiLibApplicationDao;


    @Override
    public AiControlLibDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                AI_CONTROL_LIB_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public AiControlLibDto selectById(Long id) {
        AiControlLibEntity entity = aiControlLibDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, AiControlLibDto.class));
    }

    @Override
    public List<AiControlLibDto> queryList(Map<String, Object> params) {
        List<AiControlLibEntity> list = aiControlLibDao.queryList(params);
        List<AiControlLibDto> result = CopyUtils.copyList(list, AiControlLibDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private AiControlLibDto paddingField(AiControlLibDto dto) {
        if (dto != null) {
            List<AiLibApplicationEntity> aiLibApplicationEntities = aiLibApplicationDao.findAppByLibId(dto.getId());
            List<AiLibApplicationDto> aiLibApplicationDtos = aiLibApplicationEntities.stream().map(o -> {
                AiLibApplicationDto aiLibApplicationDto = CopyUtils.copyObj(o, AiLibApplicationDto.class);
                aiLibApplicationDto.setId(o.getRefId());
                return aiLibApplicationDto;
            }).collect(Collectors.toList());
            dto.setApplications(aiLibApplicationDtos);
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return aiControlLibDao.queryTotal(params);
    }

    @Override
    public Long save(AiControlLibDto dto) {
        AiControlLibEntity entity = CopyUtils.copyObj(dto, AiControlLibEntity.class);
        entity.setId(Snowflake.nextId());
        aiControlLibDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(AiControlLibDto dto) {
        AiControlLibEntity entity = CopyUtils.copyObj(dto, AiControlLibEntity.class);
        aiControlLibDao.updateById(entity);
        CacheKit.invalidRedisCache(AI_CONTROL_LIB_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(AiControlLibDto dto) {
        AiControlLibEntity entity = CopyUtils.copyObj(dto, AiControlLibEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, aiControlLibDao);

        CacheKit.invalidRedisCache(AI_CONTROL_LIB_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        aiControlLibDao.deleteById(id);

        CacheKit.invalidRedisCache(AI_CONTROL_LIB_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
